#include "dlgstoplease.h"
#include "ui_dlgstoplease.h"
#include <QDebug>

DlgStopLease::DlgStopLease(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgStopLease)
{
    ui->setupUi(this);
    connect(ui->btn_apply, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->btn_cancel, SIGNAL(clicked()), this, SLOT(reject()));
    connect(ui->btn_privatize, SIGNAL(clicked()),this, SLOT(privatize()));
    connect(ui->btn_stop_lease, SIGNAL(clicked()), this, SLOT(stop_lease()));
    connect(ui->btn_other_reason, SIGNAL(clicked()), this, SLOT(other_reason()));
}

DlgStopLease::~DlgStopLease()
{
    delete ui;
}


void DlgStopLease::data_to_form()
{
    ui->pedit_lease_info->setPlainText(mp_dbms->get_lease_info(mqh_data));
    ui->pedit_reason->setPlainText(mqh_data.value(FLD_LS_REASON).toString());
    ui->dateEdit->setDate(mqh_data.value(FLD_LS_EDATE).toDate());
}

void DlgStopLease::form_to_data()
{
        mqh_data[FLD_LS_EDATE] = ui->dateEdit->date();
        mqh_data[FLD_LS_REASON] = ui->pedit_reason->toPlainText();
}

void DlgStopLease::setup_connection()
{

}

void DlgStopLease::accept()
{
    qDebug()<<"stop lease accepted";
    form_to_data();
    if(ui->chbox_process_spaces_pr->isChecked())
    {
        QStringList qsl_space_indices = mqh_data.value(FLD_LS_SPSLST).toStringList();

        QList<QDate> qdl_p;
        foreach(QVariant qv, qsl_space_indices)
            qdl_p.append(mqh_data.value(FLD_LS_EDATE).toDate());

        mp_dbms->privatize_lspace(mqh_data.value(FLD_LS_AINDEX), qsl_space_indices, qdl_p);
        lakhMsg()<<RUSTR("Данные о приватизации перенесены на атрибуты жилого помещения.");
    }
    return QDialog::accept();
}

void DlgStopLease::privatize()
{
    ui->chbox_process_spaces_pr->setChecked(true);
    ui->pedit_reason->setPlainText(RUSTR("Приватизация жил.пл."));
}
void DlgStopLease::stop_lease()
{
    ui->chbox_process_spaces_pr->setChecked(false);
    ui->pedit_reason->setPlainText(RUSTR("Расторжение договора"));
}

void DlgStopLease::other_reason()
{
    ui->chbox_process_spaces_pr->setChecked(true);
    ui->pedit_reason->setPlainText(RUSTR("Укажите причину прекращения действия!"));
}
